package net.zjitc.dao.impl;

import net.zjitc.dao.AdminCateGoryDao;
import net.zjitc.pojp.CateGory;
import net.zjitc.utils.db.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.log4j.Category;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by lenovo on 2017/11/15.
 */
public class AdminCateGoryDaoImpl implements AdminCateGoryDao {
    public List<CateGory> findOneList() {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select *from category";
        try {
            List<CateGory> list= (List<CateGory>) runner.query(sql,new BeanListHandler(CateGory.class));
        return  list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int insert(CateGory cateGory) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="insert into category(cname)values(?)";
        try {
            int count=runner.update(sql,cateGory.getCname());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public CateGory findByCid(String cid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select *from category where cid=?";
        try {
          CateGory cateGory= (CateGory) runner.query(sql,new BeanHandler(CateGory.class),cid);
        return cateGory;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int update(CateGory cateGory) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="update category set cname=? where cid=?";
        try {
            int count=runner.update(sql,cateGory.getCname(),cateGory.getCid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


}
